from xml.sax.handler import ContentHandler
from xml.sax import parse
import string

class HeadlineHandler(ContentHandler):    
    
    in_headline = 0
    news = 0

    def __init__(self, headlines):
        ContentHandler.__init__(self)
        global _headlines
        _headlines = headlines
        self.data = []

    def startElement(self, name, attrs):
        if name == 'newsfeed':
            self.in_headline = 1
            news = 1
            _headlines.append('nf')
        if news == 1:
            if name == 'heading':
                self.in_headline = 1
            elif name == 'first':
                self.in_headline = 1
            elif name == 'last':
                self.in_headline = 1
            elif name == 'date':
                self.in_headline = 1
            elif name == 'title':
                self.in_headline = 1
            elif name == 'text':
                self.in_headline = 1
        elif name == 'title':
            self.in_headline = 1
        elif name == 'heading':
            self.in_headline = 1
        elif name == 'subhead':
            self.in_headline = 1
        elif name == 'content':
            self.in_headline = 1
        elif name == 'item':
            self.in_headline = 1

    def endElement(self, name):
        if name == 'newsfeed':
            text = ''.join(self.data)
            self.data = []
            _headlines.append(text)
            self.in_headline = 0
        if news == 1:
            if name == 'heading':
                text = ''.join(self.data)
                self.data = []
                _headlines.append(text)
                self.in_headline = 0
            if name == 'first':
                text = ''.join(self.data)
                self.data = []
                _headlines.append(text)
                self.in_headline = 0
            if name == 'last':
                text = ''.join(self.data)
                self.data = []
                _headlines.append(text)
                self.in_headline = 0
            if name == 'date':
                text = ''.join(self.data)
                self.data = []
                _headlines.append(text)
                self.in_headline = 0
            if name == 'title':
                text = ''.join(self.data)
                self.data = []
                _headlines.append(text)
                self.in_headline = 0
            if name == 'text':
                text = ''.join(self.data)
                self.data = []
                _headlines.append(text)
                self.in_headline = 0            
        elif name == 'title':
            text = ''.join(self.data)
            self.data = []
            _headlines.append(text)
            self.in_headline = 0
        elif name == 'title':
            text = ''.join(self.data)
            self.data = []
            _headlines.append(text)
            self.in_headline = 0
        elif name == 'heading':
            text = ''.join(self.data)
            self.data = []
            _headlines.append(text)
            self.in_headline = 0
        elif name == 'subhead':
            text = ''.join(self.data)
            self.data = []
            _headlines.append(text)
            self.in_headline = 0
        elif name == 'content':
            text = ''.join(self.data)
            self.data = []
            _headlines.append(text)
            self.in_headline = 0
        elif name == 'item':
            text = ''.join(self.data)
            self.data = []
            _headlines.append(text)
            self.in_headline = 0
            
    def characters(self, string):
        if self.in_headline:
            string = string.strip()
            self.data.append(string)
            
    def getheadlines(self):
        return _headlines
